package se.topshelf.golftracker.core.domain;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Date;

/**
 * User: magnusalmeby
 * Date: 2011-04-03
 * Time: 22.50
 */
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity implements Serializable {
    public static final String COULD_NOT_REMOVE_0_DID_NOT_EXIST_ON_1 = "Could not remove {0} did not exist on {1}";
    public static final String ILLEGAL_TO_ADD_DUPLICATE_ERROR_MESSAGE = "Tried to add {0} but was already present on {1}";
    public static final String ILLEGAL_TO_REMOVE_ERROR_MESSAGE = "Tried to remove {0} but is was not present on {1}";
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "id", updatable = false, nullable = false)
    private String id;

    @Temporal(value = TemporalType.TIMESTAMP)
    @NotNull
    private Date creationTime;

    @Version
    @Temporal(value = TemporalType.TIMESTAMP)
    @NotNull
    private Date modificationTime;

    protected BaseEntity() {
        creationTime = new Date();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    public Date getModificationTime() {
        return modificationTime;
    }

    public void setModificationTime(Date modificationTime) {
        this.modificationTime = modificationTime;
    }

    public String getGuiDescription(){
        return toString();
    }

    protected void assertNotAlreadyAdded(BaseEntity entity, Collection entitySet) {
        if(entitySet.contains(entity)){
            throw new IllegalArgumentException(MessageFormat.format(ILLEGAL_TO_REMOVE_ERROR_MESSAGE, entity, this));
        }
    }
}
